layui.use('layer', function(){
    var $ = layui.jquery;
    var layer = layui.layer;

    var verifyCode = new GVerify("v_container");

    $(document).keydown(function(event){
        if(event.keyCode == 13){
            onsubmit();
        }
    });

    $("#logingBut").click(function () {
        onsubmit();
    })
    function onsubmit(){
        var user = $("#user").val();
        var password = $("#password").val();
        var verification = $("#verification").val();
        var res = verifyCode.validate(verification);
        if(user == ""){
            top.layer.msg("账号不能为空!", {icon: 2});
        }else if (password == ""){
            top.layer.msg("密码不能为空!", {icon: 2});
        }else if(verification == "") {
            top.layer.msg("验证码不能为空!", {icon: 2});
        }else if(!res){
            top.layer.msg("验证码输入错误，请重新输入!", {icon: 2});
            $("#verification").val("");
        }else{
            $.ajax({
                url: "/user/login?userName="+user+"&userPassword="+password,
                dataType: "json",
                type: "post",
                contentType: "application/json;",
                success: function (data) {
                    if(data.code == 0){
                        top.layer.msg("登陆成功!", {icon: 1});
                        window.location.href = "/home?userName="+user;
                    }else{
                        top.layer.msg("登陆失败,"+data.msg + "!", {icon: 2});
                    }
                }
            });
        }
    }

})
